-include config.mk


platform?=goodfet
mcu?=RUNCONFIG

#include `uname`.mak
GOODFET?=/dev/tty.usbser*

#For tos-bsl, use --invert-reset --invert-test
MSP430BSL?=goodfet.bsl --speed=38400
JTAG=msp430-jtag


GCCINC?=

#All of these are ?= in order ot allow overriding.
GCC?=msp430-gcc -mmcu=$(mcu)
LDFLAGS?=-mmcu=$(mcu) #-Wl,-dT ldscripts/$(mcu).x

CCEXTRA ?=  $(CFLAGS) -D$(mcu) -D$(platform) -Dplatform=$(platform) -Dboard=$(board) $(GCCINC) -I include -I platforms
CC =$(GCC) -Wall -O1 -fno-strict-aliasing -g   $(CCEXTRA)

# Available Applications
# ======================
# Below is a list of available applications and their descriptions.

# PRODUCTION:
# None have made it to production grade quality

# BETA:
# monitor -- Basic monitor
# spi -- Turns GF into USB-to-SPI adapter
# jtag -- Low level JTAG (needed by all other JTAG code)
# sbw -- Makes JTAG Spy-by-wire multiplexable

# ALPHA:
# jtag430 -- 16-bit MSP430 JTAG
# jtag430x2 -- 20-bit MSP430 JTAG
# jtagarm7 -- ARM7TDMI JTAG

# PRE-ALPHA:
#  Bus protocols:
# i2c -- Turns GF into USB-to-i2c adapter
# ejtag -- MIPS JTAG
# jtagxscale -- XScale JTAG
# openocd -- OpenOCD bitbang device
# owe -- Turns GF into USB-to-1-wire adapter

#  Microcontrollers:
# chipcon -- Chipcon radio 8051 debugging
# avr -- AVR debugger
# pic -- PIC24H/dsPIC33F debugger
# adc -- ADC10 (still specific to x2274, GoodFET32)

#  Radions:
# nrf -- Nordic RF SPI
# ccspi -- Chipcon SPI
# cc2500 -- Chipcon SPI CC2500
# atmel_radio -- Atmel radio

# Miscelaneous:
# glitch -- Glitch research tool
# smartcard -- Smartcard IO
# ps2 -- PS2 spy


# Configurations
# ==============
# This is what you need to customize to specify which apps you want in
# your firmware.  The "config" variable is just a space-delimited list
# of apps you want included.  The makefile will take the list of apps
# and include all of the proper code needed to build your desired
# firmware.  $(config) is generated by config.mk.



# Build the needed list of app and lib object files from the config
apps=

ifeq ($(platform),tilaunchpad)
libs= lib/$(platform).o lib/command.o lib/apps.o lib/msp430_serial.o
#libs+=lib/setjmp.o
else
libs= lib/$(mcu).o lib/command.o lib/dco_calib.o lib/apps.o lib/msp430.o lib/arduino.o $(extralibs)
endif

hdrs=
ERR=

# include monitor app
ifeq ($(filter monitor, $(config)), monitor)
	apps+= apps/monitor/monitor.o
	hdrs+= monitor.h
endif

# include spi app
ifeq ($(filter spi, $(config)), spi)
	apps+= apps/spi/spi.o
	hdrs+= spi.h
endif

# include twe app
ifeq ($(filter twe, $(config)), twe)
        apps+= apps/twe/twe.o
        hdrs+= twe.h
endif

# include base jtag if they specified it explicitly
ifeq ($(filter jtag, $(config)), jtag)
	ifneq ($(filter apps/jtag/jtag.o, $(apps)), apps/jtag/jtag.o)
		apps+= apps/jtag/jtag.o
		hdrs+= jtag.h
	endif
endif

# include MAX342x USB drivers.
ifeq ($(filter maxusb, $(config)), maxusb)
	ifneq ($(filter apps/usb/maxusb.o, $(apps)), apps/usb/maxusb.o)
		apps+= apps/usb/maxusb.o
		hdrs+= maxusb.h
	endif
endif

# include the sbw defs if they specified it
ifeq ($(filter sbw, $(config)), sbw)
	# if they only specify sbw, include jtag
	ifneq ($(filter apps/jtag/jtag.o, $(apps)), apps/jtag/jtag.o)
		apps+= apps/jtag/jtag.o
		hdrs+= jtag.h
	endif
	apps+= apps/jtag/sbw.o
	hdrs+= sbw.h
endif

# include jtag430 app
ifeq ($(filter jtag430, $(config)), jtag430)
	# add in base jtag code if not already
	ifneq ($(filter apps/jtag/jtag.o, $(apps)), apps/jtag/jtag.o)
		apps+= apps/jtag/jtag.o
		hdrs+= jtag.h
	endif
	# add in the jtag430asm code if needed
	ifneq ($(filter apps/jtag/jtag430asm.o, $(libs)), apps/jtag/jtag430asm.o)
		apps+= apps/jtag/jtag430asm.o
	endif
	apps+= apps/jtag/jtag430.o
	hdrs+= jtag430.h
endif

# include jtag430x2 app
ifeq ($(filter jtag430x2, $(config)), jtag430x2)
	# add in base jtag code if not already
	ifneq ($(filter apps/jtag/jtag.o, $(apps)), apps/jtag/jtag.o)
		apps+= apps/jtag/jtag.o
		hdrs+= jtag.h
	endif
	# add in the jtag430asm code if needed
	ifneq ($(filter jtag430asm.o, $(libs)), jtag430asm.o)
		libs+= apps/jtag/jtag430asm.o
	endif
	#add in the jtag430 app if not already
	ifneq ($(filter apps/jtag/jtag430.o, $(apps)), apps/jtag/jtag430.o)
		apps+= apps/jtag/jtag430.o
		hdrs+= jtag430.h
	endif
	apps+= apps/jtag/jtag430x2.o
	hdrs+= jtag430x2.h
endif

# include i2c app
ifeq ($(filter i2c, $(config)), i2c)
	apps+= apps/i2c/i2c.o
	hdrs+= i2c.h
endif

# include 1-wire app
ifeq ($(filter owe, $(config)), owe)
	apps+= apps/owe/owe.o
	hdrs+= owe.h
endif

# include jtagarm7 app
ifeq ($(filter jtagarm7, $(config)), jtagarm7)
	# add in base jtag code if not already
	ifneq ($(filter apps/jtag/jtag.o, $(apps)), apps/jtag/jtag.o)
		apps+= apps/jtag/jtag.o
		hdrs+= jtag.h
	endif
	apps+= apps/jtag/jtagarm7.o
	hdrs+= jtagarm7.h
endif

# include jtagarm7tdmi app
#ifeq ($(filter jtagarm7tdmi, $(config)), jtagarm7tdmi)
	# add in base jtag code if not already
	#ifneq ($(filter apps/jtag/jtag.o, $(apps)), apps/jtag/jtag.o)
		#apps+= apps/jtag/jtag.o
		#hdrs+= jtag.h
	#endif
	#apps+= apps/jtag/jtagarm7tdmi.o
	#hdrs+= jtagarm7tdmi.h
#endif

# include ejtag app
ifeq ($(filter ejtag, $(config)), ejtag)
	# add in base jtag code if not already
	ifneq ($(filter apps/jtag/jtag.o, $(apps)), apps/jtag/jtag.o)
		apps+= apps/jtag/jtag.o
		hdrs+= jtag.h
	endif
	apps+= apps/jtag/ejtag.o
	hdrs+= ejtag.h
endif

# include jtagxscale app
ifeq ($(filter jtagxscale, $(config)), jtagxscale)
	# add in base jtag code if not already
	ifneq ($(filter apps/jtag/jtag.o, $(apps)), apps/jtag/jtag.o)
		apps+= apps/jtag/jtag.o
		hdrs+= jtag.h
	endif
	apps+= apps/jtag/jtagxscale.o
	hdrs+= jtagxscale.h
endif

# include openocd app
ifeq ($(filter openocd, $(config)), openocd)
	# add in base jtag code if not already
	ifneq ($(filter apps/jtag/jtag.o, $(apps)), apps/jtag/jtag.o)
		apps+= apps/jtag/jtag.o
		hdrs+= jtag.h
	endif
	apps+= apps/jtag/openocd.o
	hdrs+= openocd.h
endif

# include chipcon app
ifeq ($(filter chipcon, $(config)), chipcon)
	apps+= apps/chipcon/chipcon.o
	hdrs+= chipcon.h
endif

# include avr app
ifeq ($(filter avr, $(config)), avr)
	apps+= apps/avr/avr.o
	hdrs+= avr.h
endif

# include pic app
ifeq ($(filter pic, $(config)), pic)
	apps+= apps/pic/pic.o
	hdrs+= pic.h
endif

# include adc app
ifeq ($(filter adc, $(config)), adc)
	ifeq ($(mcu), msp430f2274)
		apps+= apps/adc/adc.o
		hdrs+= adc.h
	else
		ERR= $(error The ADC app only works on GoodFET boards with the msp430x2274 processor)
	endif
endif

# include chipcon radio spi app
ifeq ($(filter ccspi, $(config)), ccspi)
	apps+= apps/radios/ccspi.o
	hdrs+= ccspi.h
endif

# include chipcon CC2500 radio spi app
ifeq ($(filter cc2500, $(config)), cc2500)
	apps+= apps/radios/cc2500.o
	hdrs+= cc2500.h
endif

# include nrf app
ifeq ($(filter nrf, $(config)), nrf)
	apps+= apps/radios/nrf.o
	hdrs+= nrf.h
endif

# include atmel_radio app
ifeq ($(filter atmel_radio, $(config)), atmel_radio)
	apps+= apps/radios/atmel_radio.o
	hdrs+= atmel_radio.h
endif

# include glitch app
ifeq ($(filter glitch, $(config)), glitch)
	apps+= apps/glitch/glitch.o
	hdrs+= glitch.h
endif

# include smartcard app
ifeq ($(filter smartcard, $(config)), smartcard)
	apps+= apps/smartcard/smartcard.o
	hdrs+= smartcard.h
endif

# include plugin app
ifeq ($(filter plugin, $(config)), plugin)
	apps+= apps/plugin.o
	hdrs+= plugin.h
endif

# include jscan app
ifeq ($(filter jscan, $(config)), jscan)
	apps+= apps/jscan/jscan.o
	hdrs+= jscan.h
endif

# Rules

app= goodfet

all: $(app).hex

lib/.o: config
	./configure
	false
lib/RUNCONFIG.o:
	./configure
	false
config:
	cp platforms/$(platform).h include/config.h
appsfiles:
	./gen_apps $(hdrs)
err:;$(ERR)
builddate:
	./gen_builddate_h
goodfet.hex: goodfet

run:
	../client/goodfet.msp430 test

avrinstall: $(app).hex
	#to be merged
ifdef AVR_PLATFORM
	avrdude -V -F -c stk500v1 -p $(AVR_PLATFORM) -b 57600 -P $(GOODFET) -U flash:w:$(app).hex
else
	avrdude -V -F -c stk500v1 -p m328p -b 57600 -P $(GOODFET) -U flash:w:$(app).hex
endif

ifeq ($(platform),tilaunchpad)
install: $(app).hex
	$(JTAG) -e -p $(app).hex
verify:
	$(JTAG) -v $(app).hex
erase:
	$(JTAG) -e $(app).hex
else
install: $(app).hex
	#Note that 'make install' no longer erases the chip.
	#Use 'make reinstall' if you want that.
	$(MSP430BSL) -p $(app).hex
reinstall: $(app).hex
	$(MSP430BSL) -e -p $(app).hex
installinfo:
	$(MSP430BSL) -P $(app).hex -p info.txt || true  #MSP430F2xx targets only, inelegant.
verify:
	$(MSP430BSL) -P $(app).hex -v $(app).hex
erase:
	$(MSP430BSL) -e

endif
dumpinfo:
	$(MSP430BSL) --dumpinfo
$(app).c: config builddate appsfiles err
$(app): $(app).o $(libs) $(apps) 
armlink: $(app).o $(libs) $(apps) $(platformlibs)
	arm-none-eabi-ld -v -Tlinkers/stm32f407.cmd -nostartfiles -o goodfet.elf $(app).o $(libs) $(apps)
	arm-none-eabi-objcopy -Obinary goodfet.elf goodfet.bin
ifeq ($(platform),donbfet)
$(app).hex: $(app)
	avr-objcopy -j .text -j .data -O ihex goodfet goodfet.hex
else
$(app).hex: $(app)
	msp430-objcopy goodfet -O ihex goodfet.hex
endif
m4s: $(app).hex
	msp430-objdump -D -m msp430 $(app).hex | m4s init
clean:
	rm -f *.o $(app) $(app).hex $(libs) $(apps) lib/apps.c include/config.h include/builddate.h include/apps.h
docs:
	doxygen
pushdocs: docs
	rsync --exclude .svn -ave ssh doc/html/* travisutk,goodfet@web.sourceforge.net:htdocs/docs/
.FAKE: docs

